package com.xbdun.service.bmp.facade;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.xbdun.common.api.Client;
import com.xbdun.common.api.XbdUnStringResult;
import com.xbdun.common.exceptions.BizException;
import com.xbdun.common.util.CommonUtil;
import com.xbdun.facade.bmp.request.system.RolePagerRequest;
import com.xbdun.facade.bmp.request.system.RoleRequest;
import com.xbdun.facade.bmp.result.system.RolePagerResult;
import com.xbdun.facade.bmp.result.system.RoleResult;
import com.xbdun.facade.bmp.service.system.RoleFacade;
import com.xbdun.service.bmp.service.system.RoleService;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: RoleFacadeImpl
 * @Description: 角色信息
 * @author 伍文强
 * @date 2017年5月11日16:56:17
 */
@Component
@Slf4j
public class RoleFacadeImpl implements RoleFacade {
	
	@Autowired
	private RoleService roleService;

	/**
	 * @Title: addRole
	 * @Description: 新增角色基本信息
	 * @param request 请求参数
	 * @throws Exception 设定文件
	 * @return void 返回类型
	 */
	public XbdUnStringResult addRole(RoleRequest request,Client client) {
		XbdUnStringResult result = new XbdUnStringResult();
		try {
			log.info("start - 新增角色初始参数,{}", request);
			CommonUtil.validateClient(client);
			addRoleChecked(request);
			roleService.addRole(request,client);
		} catch (Exception ex) {
			result.setBizException(ex);
			log.error("新增角色信息失败,入参:{},异常:{}", request, ex);
		} finally {
			log.info("end - 新增角色结果:{}", result);
		}
		return result;
	}

	// 检测角色信息
	public void addRoleChecked(RoleRequest request) {
		if (null == request)
			throw BizException.PARAM_CHKE_FAIL.newInstance("请求对象不能为空");
		StringBuilder sb = new StringBuilder();
		if (StringUtils.isBlank(request.getRoleName())) {
			sb.append("角色名称不能为空;");
		}
		if (StringUtils.isBlank(request.getRoleShort())) {
			sb.append("角色缩写不能为空;");
		}
		if (sb.length() > 0) {
			throw BizException.PARAM_CHKE_FAIL.newInstance("%s", sb.toString());
		}
	}
	
	/**
	* @Title: modifyRole
	* @Description: 修改角色信息
	* @param request	请求参数
	* @return XbdUnStringResult    返回类型
	 */
	public XbdUnStringResult modifyRole(RoleRequest request,Client client) {
		XbdUnStringResult result = new XbdUnStringResult();
		try{
			log.info("start - 修改角色信息入参:" + request);
			if(null == request || request.getId() <= 0) {
				throw BizException.PARAM_CHKE_FAIL.newInstance("角色ID不能小于等于零");
			}
			CommonUtil.validateClient(client);
			addRoleChecked(request);
			roleService.modifyRole(request,client);	
		}catch(Exception e){
			result.setBizException(e);
			log.error("修改角色信息失败,入参:{},异常:{}",request,e);
		}finally{
			log.info("end - 修改角色信息入参{}",result);
		}
		return result;
	}

	/**
	* @Title: getRole
	* @Description: 根据角色ID查询角色基本信息
	* @param id
	* @throws Exception    设定文件
	* @return RoleResult    返回类型
	 */
	public RoleResult getRole(long id,Client client){
		RoleResult result = new RoleResult();
		try{
			log.info("start - 获取角色信息入参:" + id);
			CommonUtil.validateClient(client);
			result.setData(roleService.getRole(id));
		}catch(Exception e){
			result.setBizException(e);
			log.error("获取角色信息失败,入参:{},异常:{}",id,e);
		}
		return result;
	}
	
	/**
	* @Title: queryPager
	* @Description: 角色分页查询
	* @param request	请求参数
	* @param client		客户端
	* @throws Exception    设定文件
	* @return Pager<RoleResponse>    返回类型
	 */
	public RolePagerResult queryPager(RolePagerRequest request,Client client){
		RolePagerResult result = new RolePagerResult();
		try{
			log.info("start - 获取角色列表:" + request);
			
			CommonUtil.validateClient(client);
			result.setData(roleService.queryPager(request, client));
		}catch(Exception e){
			result.setBizException(e);
			log.error("查询当前机构的角色，入参{},异常:{}",JSON.toJSONString(request),e);
		}finally{
			log.info("end - 获取角色列表结果:" + result);
		}
		return result;
	}

	public XbdUnStringResult deleteRole(Long id, Client client) {
		XbdUnStringResult result = new XbdUnStringResult();
		try{
			log.info("start - 删除角色信息入参:" + id);
			if(id <= 0) {
				throw BizException.PARAM_CHKE_FAIL.newInstance("角色ID不能小于等于零");
			}
			CommonUtil.validateClient(client);
			roleService.deleteRole(id,client);	
		}catch(Exception e){
			result.setBizException(e);
			log.error("修改角色信息失败,入参:{},异常:{}",id,e);
		}finally{
			log.info("end - 修改角色信息入参{}",result);
		}
		return result;
	}
}
